<template>
	<view class="container bk-little-gray" style="min-height: 100vh;">
		<view class="step_one" v-if="partitions.length > 1">
			<view class="popup_group">
				<view class="popup_group_item">
					<view class="popup_group_item_label">选择分区</view>
					<view class="popup_group_item_value">
						<picker mode="selector" :value="currPartition" :range="partitions" range-key="partitionName" @change="onSelectPartition">
							<view class="uni-input lay-c-c pd-h-xxl">{{partitions[currPartition].partitionName}}</view>
						</picker>
					</view>
				</view>
			</view>
		</view>
		
		<view class="step_one">
			<view class="popup_group" v-for="(lotteryRule, lotteryRuleIdx) in partitions[currPartition].lotteryRules" :key="lotteryRuleIdx">
				<!--
				<view class="lay-sb-c-c mg-v-md">
					<text class="title">规则{{index+1}}</text>
					<button class="mini-btn" type="warn" size="mini" @click="delLotteryRule(index)">删除</button>
				</view>
				-->
				<view class="popup_group_item">
					<view class="popup_group_item_label title">规则{{lotteryRuleIdx+1}}</view>
					<button class="mini-btn" type="warn" size="mini" @click="delLotteryRule(lotteryRuleIdx)">删除</button>
				</view>
				
				<template v-if="editingRule == lotteryRuleIdx">
					<view class="popup_group_item">
						<view class="popup_group_item_label">模式：</view>
						<radio-group @change="onSeatModeChange($event, lotteryRuleIdx)" style="display: flex;">
							<label style="display: flex;">
								<view><radio value=2 :checked="2==lotteryRule.seatMode" /></view>
								<view>摇号</view>
							</label>
							<label style="display: flex; margin-left: 20rpx;">
								<view><radio value=1 :checked="1==lotteryRule.seatMode" /></view>
								<view>自选</view>
							</label>
						</radio-group>
					</view>
					
					<view class="popup_group_item" v-if="lotteryRule.seatMode==2">
						<view class="popup_group_item_label">数量(摇N选1)：</view>
						<view class="popup_group_item_value">
							<!-- <input v-model="lotteryRule.lotteryNumber" type="number" maxlength="1"/> -->
							<picker mode="selector" :value="lotteryRule.lotteryNumber" :range="[1,2,3,4,5]" @change="onSelectLotteryNumber($event, lotteryRuleIdx)">
								<view class="uni-input lay-c-c pd-h-xxl">{{lotteryRule.lotteryNumber}}</view>
							</picker>
						</view>
					</view>
					
					<view class="popup_group_item">
						<view class="popup_group_item_label">钓位：</view>
						<radio-group @change="onSeatRangeTypeChange($event, lotteryRuleIdx)" style="display: flex;">
							<label style="display: flex;">
								<view><radio value=0 :checked="0==lotteryRule.seatRangeType" /></view>
								<view>全部</view>
							</label>
							<label style="display: flex; margin-left: 20rpx;">
								<view><radio value=1 :checked="1==lotteryRule.seatRangeType" /></view>
								<view>单号</view>
							</label>
							<label style="display: flex; margin-left: 20rpx;">
								<view><radio value=2 :checked="2==lotteryRule.seatRangeType" /></view>
								<view>双号</view>
							</label>
						</radio-group>
					</view>
					
					<view class="popup_group_item">
						<view class="mg-b-md" style="min-width: 140rpx;">周几：</view>
						<checkbox-group @change="daysOfWeekChange($event, lotteryRuleIdx)">
							<block v-for="(dayOfWeek, dayOfWeekIdx) in lotteryRule.daysOfWeek" :key="dayOfWeekIdx">
								<label class="pd-sm" style="display: inline-block;">
									<checkbox :value="dayOfWeek.day" :checked="dayOfWeek.checked==1" />{{dayOfWeek.name}}
								</label>
								<br v-if="dayOfWeekIdx==3">
							</block>
						</checkbox-group>
					</view>
					
					<!--
					<view class="popup_group_item">
						<view class="popup_group_item_label">优化级(数字小优先)：</view>
						<view class="popup_group_item_value"><input v-model="setFormData.prepairMinutes" type="number" maxlength="4" placeholder="优化级"/></view>
					</view>
					-->
					
					<!-- <view class="popup_group_item" v-if="lotteryRuleIdx == partitions[currPartition].lotteryRules.length-1">
						<view class="mg-b-md" style="min-width: 140rpx;"></view>
						<button class="btn bk-blue white" @click="addRule">添加规则</button>
					</view> -->
				
				</template>
				
				<template v-else>
					<view class="popup_group_item">
						<view class="popup_group_item_label">模式：</view>
						<view>{{2==lotteryRule.seatMode ? "摇号" : "自选"}}</view>
					</view>
					
					<view class="popup_group_item" v-if="lotteryRule.seatMode==2">
						<view class="popup_group_item_label">数量(摇N选1)：</view>
						<view class="popup_group_item_value">{{lotteryRule.lotteryNumber}}</view>
					</view>
					
					<view class="popup_group_item">
						<view class="popup_group_item_label">钓位：</view>
						<view>{{0==lotteryRule.seatRangeType ? "全部" : (1==lotteryRule.seatRangeType ? "单号" : "双号")}}</view>
					</view>
					
					<view class="popup_group_item">
						<view class="mg-b-md" style="min-width: 140rpx;">周几：</view>
						<view>
							<block v-for="(dayOfWeek, dayOfWeekIdx) in lotteryRule.daysOfWeek" :key="dayOfWeekIdx">
								<text v-if="dayOfWeek.checked==1">{{dayOfWeek.name}}</text>
								<text v-if="dayOfWeek.checked==1 && dayOfWeekIdx != 6" class="pd-r-sm">,</text>
							</block>
						</view>
					</view>
					
					<!--
					<view class="popup_group_item">
						<view class="popup_group_item_label">优化级(数字小优先)：</view>
						<view class="popup_group_item_value"><input v-model="setFormData.prepairMinutes" type="number" maxlength="4" placeholder="优化级"/></view>
					</view>
					-->
					
					<!-- <view class="popup_group_item" v-if="lotteryRuleIdx == partitions[currPartition].lotteryRules.length-1">
						<view class="mg-b-md" style="min-width: 140rpx;"></view>
						<button class="btn bk-blue white" @click="addRule">添加规则</button>
					</view> -->
				
				</template>
			</view>
			
			<view class="mg-b-md pd-md lay-c-c">
				<button class="btn bk-red white" @click="onTapAddRule">{{btnTitle}}</button>
				<!--<button class="btn bk-blue white" style="width: 40%;" @click="onTapSave">保存</button> -->
			</view>
			
		</view>
	</view>
</template>

<script>
const app = getApp();
import { navigateTo, serialize, setStorage, getStorage, removeStorage, Loading, hideLoading, Modal } from '@/libs/uniApi.js';

import store from '@/store';
import {genDefaultPartition, correctSeats, MyTip} from '@/components/ssty/common.js';

import {queryLotteryRules, addLotteryRule, softDeleteLotteryRule} from '@/api/fsh.js'

export default {
	data() {
		return {
			// 摇号规则列表，从属于分区
			partitions: [{
				pondId: 0,
				partitionId: 0,
				partitionName: '',
				lotteryRules: [{
					seatMode: 2, // 1-选位, 2-摇号
					lotteryNumber: 1, // 每次摇多少供选择
					seatRangeType: 0, // 0-全部, 1-单号, 2-双号
					daysOfWeek: [
						{name:'周一', checked:1, day: 1},{name:'周二', checked:1, day: 2},
						{name:'周三', checked:1, day: 3},{name:'周四', checked:1, day: 4},
						{name:'周五', checked:1, day: 5},{name:'周六', checked:1, day: 6},
						{name:'周日', checked:1, day: 7}]
				}]
			}],
			currPartition: 0, // 当前选择的分区
			editingRule: null, // 当前正在编辑的规则
			btnTitle: '添加规则',
		};
	},
	onLoad(opt) {
		console.log("lotteryRule::onLoad, opt=", opt);
		this.prodId = opt.product_id;
		this.getRules();
	},
	methods: {
		getRules() {
			queryLotteryRules(this.prodId).then(res=>{
				this.partitions = res.entity;
				this.original_partitions = JSON.parse(JSON.stringify(this.partitions));
			}).catch(err=>{
				console.error(err);
				this.$util.Tips({title: err.msg}, {tab: 3, url: 1}); // 提示错误并返回上一页面 uni-app170\utils
			})
		},
		
		onSelectPartition(e) {
			//console.log("onSelectPartition:", e);
			this.currPartition = parseInt(e.detail.value);
		},
		
		onSelectLotteryNumber(e, lotteryRuleIdx) {
			//this.currPartition = parseInt(e.detail.value);
			this.partitions[this.currPartition].lotteryRules[lotteryRuleIdx].lotteryNumber = parseInt(e.detail.value) + 1;
		},
		
		delLotteryRule(lotteryRuleIdx) {
			if(this.partitions[this.currPartition].lotteryRules[lotteryRuleIdx].id) {
				uni.showLoading({
					title: "删除中..."
				})
				softDeleteLotteryRule(this.partitions[this.currPartition].pondId, this.partitions[this.currPartition].lotteryRules[lotteryRuleIdx].id, 1).then(res=>{
					this.partitions[this.currPartition].lotteryRules.splice(lotteryRuleIdx, 1);
					MyTip("删除成功");
				}).catch(err=>{
					MyTip("删除失败: "+err.msg)
				})
			}
			else
				this.partitions[this.currPartition].lotteryRules.splice(lotteryRuleIdx, 1);
		},
		
		onSeatModeChange(e, lotteryRuleIdx) {
			let v = parseInt(e.detail.value);
			this.partitions[this.currPartition].lotteryRules[lotteryRuleIdx].seatMode = v;
		},
		
		onSeatRangeTypeChange(e, lotteryRuleIdx) {
			let v = parseInt(e.detail.value);
			this.partitions[this.currPartition].lotteryRules[lotteryRuleIdx].seatRangeType = v;
		},
		
		daysOfWeekChange(e, lotteryRuleIdx) {
			console.log("daysOfWeekChange, lotteryRuleIdx="+lotteryRuleIdx, e)
			this.partitions[this.currPartition].lotteryRules[lotteryRuleIdx].daysOfWeek.forEach(item=>item.checked=0);
			e.detail.value.forEach(day=>{
				let i = parseInt(day) - 1;
				this.partitions[this.currPartition].lotteryRules[lotteryRuleIdx].daysOfWeek[i].checked=1;
			})
		},
		
		onTapAddRule() {
			if(this.btnTitle == '添加规则') {
				this.partitions[this.currPartition].lotteryRules.push({
					seatMode: 2, // 1-选位, 2-摇号 
					lotteryNumber: 1, // 每次摇多少供选择
					seatRangeType: 0, // 0-全部, 1-单号, 2-双号
					daysOfWeek: [
						{name:'周一', checked:1, day: 1},{name:'周二', checked:1, day: 2},
						{name:'周三', checked:1, day: 3},{name:'周四', checked:1, day: 4},
						{name:'周五', checked:1, day: 5},{name:'周六', checked:1, day: 6},
						{name:'周日', checked:1, day: 7}]
				});
			
				this.editingRule = this.partitions[this.currPartition].lotteryRules.length - 1;
				this.btnTitle = '保存';
			}
			else {
				let i = this.partitions[this.currPartition].lotteryRules.length - 1;
				let {pondId, partitionId} = this.partitions[this.currPartition];
				let {seatMode, seatRangeType, lotteryNumber, daysOfWeek} = this.partitions[this.currPartition].lotteryRules[i];
				let applyToDaysOfWeek = '';
				for(let i = 0; i < daysOfWeek.length; i++)
					applyToDaysOfWeek += daysOfWeek[i].checked?'1':'0';
				uni.showLoading({
					title: "保存中..."
				})
				addLotteryRule(pondId, partitionId, seatMode, seatRangeType, lotteryNumber, applyToDaysOfWeek).then(res=>{
					this.partitions[this.currPartition].lotteryRules[i].id = res.entity;
					uni.hideLoading();
					this.btnTitle = '添加规则';
					this.editingRule = null;
					MyTip("保存成功");
				}).catch(err=>{
					uni.hideLoading();
					MyTip("保存失败: "+err.msg)
				})
				
			}
		},
		
		// onTapSave() {
		// 	console.log("partitons:", this.partitions);
			
		// 	// 识别删除的
		// 	let del = this.original_partitions[this.currPartition].lotteryRules.filter(a=>this.partitions[this.currPartition].lotteryRules.find(b=>b.id==a.id)<0);
			
		// 	// 识别添加的和删除的
		// }
		
	}
};
</script>

<style lang="scss" scoped>
@import './scss/index.scss';

@import "@/leui.scss";

page {
	height: 100%;
}

</style>
